<template>
  <div class="login-container">
    <!-- 粒子背景 -->
    <canvas ref="particleCanvas" class="particle-canvas"/>

    <!-- 左侧个人展示区域 -->
    <div class="left-section">
      <div class="content-overlay">
        <!-- 动态背景装饰 -->
        <div class="floating-elements">
          <div class="code-snippet snippet-1">console.log('Hello World!');</div>
          <div class="code-snippet snippet-2">const magic = true;</div>
          <div class="code-snippet snippet-3">// 创意无限</div>
          <div class="emoji-float emoji-1">🚀</div>
          <div class="emoji-float emoji-2">💡</div>
          <div class="emoji-float emoji-3">⭐</div>
          <div class="emoji-float emoji-4">🎨</div>
          <div class="geometric-shape shape-1"/>
          <div class="geometric-shape shape-2"/>
          <div class="geometric-shape shape-3"/>
        </div>

        <!-- 主要内容 -->
        <div class="main-content">
          <!-- 个人头像和介绍 -->
          <div class="profile-section">
            <div class="avatar-container">
              <!-- <img src="/vite.svg?height=120&width=120" alt="Avatar" class="profile-avatar"> -->
              <div class="status-indicator"/>
            </div>
            <h1 class="welcome-title">欢迎来到我的创意空间</h1>
            <p class="personal-intro">全栈开发者 | 创意实现者 | 代码艺术家</p>
          </div>

          <!-- 技能标签云 -->
          <div class="skills-cloud">
            <span v-for="(skill, index) in skills" :key="index" class="skill-tag" :style="{ animationDelay: index * 0.2 + 's' }">
              {{ skill }}
            </span>
          </div>

          <!-- 项目统计 -->
          <div class="project-stats">
            <div v-for="(stat, index) in personalStats" :key="index" class="stat-card">
              <div class="stat-icon">{{ stat.icon }}</div>
              <div class="stat-info">
                <div class="stat-number">{{ stat.number }}</div>
                <div class="stat-label">{{ stat.label }}</div>
              </div>
            </div>
          </div>

          <!-- 个人座右铭 -->
          <div class="motto-section">
            <div class="quote-mark">"</div>
            <p class="motto-text">{{ currentMotto }}</p>
            <div class="motto-author">- 我的编程哲学</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="right-section">
      <div class="form-container">
        <el-card class="login-card" shadow="always">
          <div class="card-header">
            <div class="login-logo">
              <div class="logo-circle">
                <span class="logo-text">{{ logoText }}</span>
              </div>
            </div>
            <h2 class="login-title">{{ greetingText }}</h2>
            <p class="login-subtitle">准备好开始今天的创作了吗？</p>
          </div>

          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            @submit.prevent="handleLogin"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="输入你的用户名"
                size="large"
                :prefix-icon="User"
                class="form-input"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="输入你的密码"
                size="large"
                :prefix-icon="Lock"
                show-password
                class="form-input"
              />
            </el-form-item>

            <div class="form-options">
              <el-checkbox v-model="loginForm.remember" class="remember-checkbox">
                记住我
              </el-checkbox>
              <el-link type="primary" class="forgot-link">忘记密码了？</el-link>
            </div>

            <el-button
              type="primary"
              size="large"
              class="login-button"
              :loading="isLoading"
              native-type="submit"
              @click="handleLogin"
            >
              <template v-if="!isLoading">
                <i class="el-icon-right login-icon"/>
                {{ loginButtonText }}
              </template>
              <template v-else>
                正在进入创意空间...
              </template>
            </el-button>
          </el-form>

          <div class="card-footer">
            <p class="register-text">
              第一次来这里?
              <el-link type="primary" class="register-link">创建账号</el-link>
            </p>
            <div class="social-login">
              <p class="social-text">或者使用</p>
              <div class="social-buttons">
                <el-button circle class="social-btn github-btn">
                  <i class="fab fa-github"/>
                </el-button>
                <el-button circle class="social-btn google-btn">
                  <i class="fab fa-google"/>
                </el-button>
                <el-button circle class="social-btn wechat-btn">
                  <i class="fab fa-weixin"/>
                </el-button>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@w/apis/user'
import md5 from 'md5'

import router from '@/router'

import useUserStore from '~stores/module/user'
import { loginFormConfig } from '~utils/config'

const { setUserInfo } = useUserStore()

// 响应式数据
const loginFormRef = ref()
const particleCanvas = ref()
const isLoading = ref(false)

const loginForm = reactive({
  username: loginFormConfig.username,
  password: loginFormConfig.password,
  remember: false
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
    // { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

// 个人技能标签
const skills = [
  'Vue.js', 'React', 'Node.js', 'Python', 'TypeScript',
  'UI/UX', '创意设计', '全栈开发', 'AI集成', '开源贡献'
]

// 个人统计数据
const personalStats = [
  { icon: '🚀', number: '50+', label: '项目完成' },
  { icon: '⭐', number: '1K+', label: 'GitHub Stars' },
  { icon: '☕', number: '999+', label: '杯咖啡' }
]

// 座右铭列表
const mottos = [
  '代码是诗歌，bug是韵脚',
  '用技术改变世界，用创意点亮生活',
  '每一行代码都是对未来的投资',
  '创造不仅仅是工作，更是生活方式'
]

// 动态文本
const currentMotto = ref(mottos[0])
const logoText = ref('DEV')
const greetingText = ref('嗨，欢迎回来！')
const loginButtonText = ref('开始创作')

// 动态更新座右铭
let mottoIndex = 0
setInterval(() => {
  mottoIndex = (mottoIndex + 1) % mottos.length
  currentMotto.value = mottos[mottoIndex]
}, 4000)

// 根据时间动态更新问候语
const updateGreeting = () => {
  const hour = new Date().getHours()
  if (hour < 6) {
    greetingText.value = '夜猫子，还在coding？'
    loginButtonText.value = '继续创作'
  } else if (hour < 12) {
    greetingText.value = '早安，创作者！'
    loginButtonText.value = '开始新的一天'
  } else if (hour < 18) {
    greetingText.value = '下午好，灵感家！'
    loginButtonText.value = '继续创作'
  } else {
    greetingText.value = '晚上好，梦想家！'
    loginButtonText.value = '夜间模式启动'
  }
}

// 粒子动画相关
let particles = []
let animationId = null

// 登录处理
const handleLogin = async() => {
  if (!loginFormRef.value) return

  try {
    const valid = await loginFormRef.value.validate()
    if (!valid) return

    isLoading.value = true

    const { data } = await login({
      username: loginForm.username,
      password: md5(loginForm.password)
    })
    console.log('🚀 ~ handleLogin ~ data:', data)
    setUserInfo(data)

    router.push('/')

  } catch (error) {
    console.log('🚀 ~ handleLogin ~ error:', error)
  } finally {
    isLoading.value = false
  }
}

// 粒子系统初始化
const initParticles = () => {
  const canvas = particleCanvas.value
  if (!canvas) return

  canvas.width = window.innerWidth
  canvas.height = window.innerHeight

  particles = []
  const particleCount = Math.min(Math.floor(window.innerWidth / 12), 60)

  const colors = [
    'rgba(147, 51, 234, 0.7)',
    'rgba(219, 39, 119, 0.7)',
    'rgba(236, 72, 153, 0.7)',
    'rgba(59, 130, 246, 0.7)',
    'rgba(16, 185, 129, 0.7)'
  ]

  for (let i = 0; i < particleCount; i++) {
    particles.push({
      x: Math.random() * canvas.width,
      y: Math.random() * canvas.height,
      size: Math.random() * 3 + 1,
      speedX: Math.random() * 1.5 - 0.75,
      speedY: Math.random() * 1.5 - 0.75,
      color: colors[Math.floor(Math.random() * colors.length)]
    })
  }
}

// 粒子动画
const animateParticles = () => {
  const canvas = particleCanvas.value
  if (!canvas) return

  const ctx = canvas.getContext('2d')
  ctx.clearRect(0, 0, canvas.width, canvas.height)

  particles.forEach((particle, index) => {
    particle.x += particle.speedX
    particle.y += particle.speedY

    if (particle.x > canvas.width) particle.x = 0
    else if (particle.x < 0) particle.x = canvas.width

    if (particle.y > canvas.height) particle.y = 0
    else if (particle.y < 0) particle.y = canvas.height

    ctx.beginPath()
    ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2)
    ctx.fillStyle = particle.color
    ctx.fill()

    // 连接附近粒子
    for (let i = index + 1; i < particles.length; i++) {
      const dx = particle.x - particles[i].x
      const dy = particle.y - particles[i].y
      const distance = Math.sqrt(dx * dx + dy * dy)

      if (distance < 100) {
        ctx.beginPath()
        ctx.strokeStyle = `rgba(255, 255, 255, ${0.1 * (1 - distance / 100)})`
        ctx.lineWidth = 0.5
        ctx.moveTo(particle.x, particle.y)
        ctx.lineTo(particles[i].x, particles[i].y)
        ctx.stroke()
      }
    }
  })

  animationId = requestAnimationFrame(animateParticles)
}

// 窗口大小调整
const handleResize = () => {
  const canvas = particleCanvas.value
  if (canvas) {
    canvas.width = window.innerWidth
    canvas.height = window.innerHeight
    initParticles()
  }
}

onMounted(() => {
  updateGreeting()
  initParticles()
  animateParticles()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  if (animationId) {
    cancelAnimationFrame(animationId)
  }
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css');

.login-container {
  position: relative;
  display: flex;
  overflow: hidden;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #f5576c 75%, #4facfe 100%);
}

.particle-canvas {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.left-section {
  flex: 1;
  position: relative;
  z-index: 2;
  display: none;
}

@media (width >= 1024px) {
  .left-section {
    display: block;
  }
}

.content-overlay {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  padding: 2rem;
  background: linear-gradient(135deg, rgb(102 126 234 / 90%) 0%, rgb(118 75 162 / 90%) 100%);
  inset: 0;
  backdrop-filter: blur(20px);
}

.floating-elements {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

.code-snippet {
  position: absolute;
  padding: 0.5rem 1rem;
  font-size: 0.8rem;
  font-family: 'Courier New', monospace;
  color: #0f8;
  background: rgb(0 0 0 / 30%);
  border-radius: 8px;
  animation: codeFloat 8s ease-in-out infinite;
  backdrop-filter: blur(10px);
}

.snippet-1 {
  top: 15%;
  left: 10%;
  animation-delay: 0s;
}

.snippet-2 {
  top: 70%;
  left: 75%;
  animation-delay: 3s;
}

.snippet-3 {
  top: 45%;
  left: 5%;
  animation-delay: 6s;
}

.emoji-float {
  position: absolute;
  font-size: 2rem;
  animation: emojiFloat 6s ease-in-out infinite;
}

.emoji-1 {
  top: 20%;
  right: 15%;
  animation-delay: 1s;
}

.emoji-2 {
  top: 60%;
  right: 25%;
  animation-delay: 2s;
}

.emoji-3 {
  top: 80%;
  right: 10%;
  animation-delay: 4s;
}

.emoji-4 {
  top: 35%;
  right: 5%;
  animation-delay: 5s;
}

.geometric-shape {
  position: absolute;
  background: linear-gradient(45deg, rgb(255 255 255 / 10%), rgb(255 255 255 / 5%));
  animation: shapeRotate 10s linear infinite;
}

.shape-1 {
  top: 25%;
  left: 80%;
  width: 60px;
  height: 60px;
  clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}

.shape-2 {
  top: 65%;
  left: 15%;
  width: 80px;
  height: 80px;
  clip-path: polygon(25% 0%, 100% 0%, 75% 100%, 0% 100%);
}

.shape-3 {
  top: 10%;
  left: 60%;
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

@keyframes codeFloat {
  0%, 100% {
    transform: translateY(0) translateX(0);
    opacity: 0.7;
  }

  50% {
    transform: translateY(-15px) translateX(10px);
    opacity: 1;
  }
}

@keyframes emojiFloat {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }

  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

@keyframes shapeRotate {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.main-content {
  position: relative;
  z-index: 10;
  max-width: 500px;
  text-align: center;
  color: white;
}

.profile-section {
  margin-bottom: 2rem;
}

.avatar-container {
  position: relative;
  display: inline-block;
  margin-bottom: 1rem;
}

.profile-avatar {
  width: 120px;
  height: 120px;
  border: 4px solid rgb(255 255 255 / 30%);
  border-radius: 50%;
  animation: avatarGlow 3s ease-in-out infinite;
}

.status-indicator {
  position: absolute;
  right: 10px;
  bottom: 10px;
  width: 20px;
  height: 20px;
  background: #0f8;
  border: 3px solid white;
  border-radius: 50%;
  animation: statusPulse 2s infinite;
}

@keyframes avatarGlow {
  0%, 100% {
    box-shadow: 0 0 20px rgb(255 255 255 / 30%);
  }

  50% {
    box-shadow: 0 0 30px rgb(255 255 255 / 60%);
  }
}

@keyframes statusPulse {
  0%, 100% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.2);
  }
}

.welcome-title {
  margin: 0 0 0.5rem;
  font-size: 2.5rem;
  text-shadow: 2px 2px 4px rgb(0 0 0 / 30%);
  font-weight: bold;
}

.personal-intro {
  margin: 0 0 2rem;
  font-size: 1.1rem;
  opacity: 0.9;
}

.skills-cloud {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 2rem;
}

.skill-tag {
  padding: 0.3rem 0.8rem;
  font-size: 0.8rem;
  color: white;
  background: rgb(255 255 255 / 20%);
  border-radius: 20px;
  backdrop-filter: blur(10px);
  animation: skillFloat 3s ease-in-out infinite;
}

@keyframes skillFloat {
  0%, 100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-5px);
  }
}

.project-stats {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  margin-bottom: 2rem;
  flex-wrap: wrap;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 1rem;
  min-width: 120px;
  background: rgb(255 255 255 / 15%);
  border-radius: 12px;
  gap: 0.8rem;
  backdrop-filter: blur(10px);
}

.stat-icon {
  font-size: 1.5rem;
}

.stat-number {
  font-size: 1.3rem;
  font-weight: bold;
  color: #fbbf24;
}

.stat-label {
  font-size: 0.8rem;
  opacity: 0.9;
}

.motto-section {
  position: relative;
  padding: 1.5rem;
  background: rgb(0 0 0 / 20%);
  border-radius: 12px;
  backdrop-filter: blur(10px);
}

.quote-mark {
  position: absolute;
  top: -10px;
  left: 15px;
  font-size: 3rem;
  color: rgb(255 255 255 / 30%);
}

.motto-text {
  margin: 0 0 0.5rem;
  font-size: 1rem;
  font-style: italic;
  line-height: 1.5;
}

.motto-author {
  font-size: 0.8rem;
  text-align: right;
  opacity: 0.7;
}

.right-section {
  position: relative;
  z-index: 2;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 2rem;
  flex: 1;
}

.form-container {
  width: 100%;
  max-width: 420px;
}

.login-card {
  background: rgb(255 255 255 / 95%);
  backdrop-filter: blur(20px);
  border: none;
  border-radius: 20px;
  box-shadow: 0 25px 50px rgb(0 0 0 / 15%);
}

.card-header {
  margin-bottom: 2rem;
  text-align: center;
}

.login-logo {
  margin-bottom: 1rem;
}

.logo-circle {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
  width: 60px;
  height: 60px;
  background: linear-gradient(45deg, #667eea, #764ba2);
  border-radius: 50%;
  animation: logoSpin 4s linear infinite;
}

.logo-text {
  font-size: 1.2rem;
  color: white;
  font-weight: bold;
}

@keyframes logoSpin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.login-title {
  margin: 0 0 0.5rem;
  font-size: 1.8rem;
  background: linear-gradient(45deg, #667eea, #f093fb);
  background-clip: text;
  font-weight: bold;
  -webkit-text-fill-color: transparent;
}

.login-subtitle {
  margin: 0;
  font-size: 0.95rem;
  color: #6b7280;
}

.login-form {
  margin-bottom: 1.5rem;
}

.error-alert {
  margin-bottom: 1rem;
}

.form-input {
  margin-bottom: 1rem;
}

.form-input :deep(.el-input__inner) {
  height: 48px;
  border-color: #e5e7eb;
  border-radius: 12px;
}

.form-input :deep(.el-input__inner:focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgb(102 126 234 / 20%);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.remember-checkbox :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #667eea;
  border-color: #667eea;
}

.forgot-link {
  font-size: 0.875rem;
}

.login-button {
  width: 100%;
  height: 50px;
  font-size: 1rem;
  background: linear-gradient(45deg, #667eea, #764ba2);
  border: none;
  border-radius: 12px;
  transition: all 0.3s ease;
  font-weight: 600;
}

.login-button:hover {
  background: linear-gradient(45deg, #5a67d8, #6b46c1);
  transform: translateY(-2px);
  box-shadow: 0 15px 30px rgb(102 126 234 / 40%);
}

.login-icon {
  margin-right: 0.5rem;
}

.card-footer {
  padding-top: 1rem;
  text-align: center;
  border-top: 1px solid #f3f4f6;
}

.register-text {
  margin: 0 0 1rem;
  font-size: 0.875rem;
  color: #6b7280;
}

.register-link {
  font-weight: 600;
}

.social-login {
  margin-top: 1rem;
}

.social-text {
  margin: 0 0 0.8rem;
  font-size: 0.8rem;
  color: #9ca3af;
}

.social-buttons {
  display: flex;
  justify-content: center;
  gap: 0.8rem;
}

.social-btn {
  width: 40px;
  height: 40px;
  border: none;
  transition: all 0.3s ease;
}

.github-btn {
  color: white;
  background: #333;
}

.github-btn:hover {
  background: #24292e;
  transform: translateY(-2px);
}

.google-btn {
  color: white;
  background: #db4437;
}

.google-btn:hover {
  background: #c23321;
  transform: translateY(-2px);
}

.wechat-btn {
  color: white;
  background: #1aad19;
}

.wechat-btn:hover {
  background: #179b16;
  transform: translateY(-2px);
}

@media (width <= 1023px) {
  .login-container {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #f5576c 75%, #4facfe 100%);
  }

  .right-section {
    flex: none;
    width: 100%;
  }

  .welcome-title {
    font-size: 2rem;
  }
}
</style>
